package com.huatai.user.controller;


import com.huatai.common.api.R;
import com.huatai.common.controller.BaseController;
import com.huatai.common.entity.SysOperLog;
import com.huatai.common.log.annotation.Log;
import com.huatai.common.log.enums.BusinessType;
import com.huatai.common.util.ExcelUtil;
import com.huatai.user.dto.LogIdsDto;
import com.huatai.user.service.ISysOperLogService;
import com.huatai.user.vo.LogParamInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author zsj
 */
@RestController
@RequestMapping("operlog")
@Api(value = "日志管理", tags = "日志管理")
public class SysOperlogController extends BaseController {
	@Autowired
	private ISysOperLogService operLogService;

	@PostMapping("/list")
	@ApiOperation(value = "获取日志列表", notes = "获取日志列表")
	public R list(@RequestBody LogParamInfo logParamInfo) {
		return R.data(operLogService.selectOperLogListByPage(logParamInfo));
	}

	@Log(title = "操作日志", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ApiOperation(value = "导出日志", notes = "导出日志")
	public void export(HttpServletResponse response , @RequestBody LogIdsDto logIdsDto) {
		List<SysOperLog> list = operLogService.selectLogList(logIdsDto);
		//截取请求参数为2000字符
		for (SysOperLog sysOperLog : list) {
			if (sysOperLog.getOperParam() != null && sysOperLog.getOperParam().length() > 2000) {
				sysOperLog.setOperParam(sysOperLog.getOperParam().substring(0, 2000));
			}
		}
		ExcelUtil<SysOperLog> util = new ExcelUtil<>(SysOperLog.class);
		util.exportExcel(response, list, "操作日志");
	}
	@Log(title = "操作日志", businessType = BusinessType.DELETE)
	@PostMapping("/deleteLogList")
	@ApiOperation(value = "批量删除操作日志", notes = "批量删除操作日志")
	public R remove(@RequestBody LogIdsDto logIdsDto) {
		return R.data(operLogService.deleteOperLogByIds(logIdsDto.getOperIds()));
	}

	@Log(title = "操作日志", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clean")
	@ApiOperation(value = "清空日志", notes = "清空日志")
	public R clean() {
		operLogService.cleanOperLog();
		return R.success("success");
	}

	//    @InnerAuth
	@PostMapping
	@ApiOperation(value = "添加日志", notes = "添加日志")
	public R add(@RequestBody SysOperLog operLog) {
		return R.data(operLogService.insertOperlog(operLog));
	}
}
